package io.imp.modules.sys.controller;

import java.util.Arrays;
import java.util.Map;

import io.imp.common.utils.DESedeUtils;
import io.imp.common.validator.ValidatorUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.imp.modules.sys.entity.SysJdbcEntity;
import io.imp.modules.sys.service.SysJdbcService;
import io.imp.common.utils.PageUtils;
import io.imp.common.utils.R;



/**
 * 
 *
 * @author wanghui
 * @email ${email}
 * @date 2019-04-30 16:52:15
 */
@RestController
@RequestMapping("sys/sysjdbc")
public class SysJdbcController {
    @Autowired
    private SysJdbcService sysJdbcService;
    /*@Autowired
    private DynamicDataSourceConfig dynamicDataSourceConfig;*/

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("sys:sysjdbc:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = sysJdbcService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("sys:sysjdbc:info")
    public R info(@PathVariable("id") Long id){
        SysJdbcEntity sysJdbc = sysJdbcService.getById(id);
        sysJdbc.setJdbcPwd(DESedeUtils.decrypt(sysJdbc.getJdbcPwd()));
        return R.ok().put("sysJdbc", sysJdbc);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("sys:sysjdbc:save")
    public R save(@RequestBody SysJdbcEntity sysJdbc) throws Exception {
        sysJdbc.setJdbcPwd(DESedeUtils.encrypt(sysJdbc.getJdbcPwd()));
        sysJdbcService.save(sysJdbc);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("sys:sysjdbc:update")
    public R update(@RequestBody SysJdbcEntity sysJdbc) throws Exception{
        ValidatorUtils.validateEntity(sysJdbc);
        sysJdbc.setJdbcPwd(DESedeUtils.encrypt(sysJdbc.getJdbcPwd()));
        sysJdbcService.updateById(sysJdbc);
        
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("sys:sysjdbc:delete")
    public R delete(@RequestBody Long[] ids){
        sysJdbcService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }
    /**
     * 重载
     */
    @RequestMapping("/overload")
    @RequiresPermissions("sys:sysjdbc:overload")
    public R overload(){
        /*DynamicContextHolder.poll();
        dynamicDataSourceConfig.reload();*/
        return R.ok();
    }
    /**
     * 重载
     */
    @RequestMapping("/locking")
    @RequiresPermissions("sys:sysjdbc:locking")
    public R locking(@RequestBody SysJdbcEntity sysJdbc){
        /*DynamicContextHolder.poll();
        DynamicContextHolder.DS_NAME = sysJdbc.getJdbcName();*/
        return R.ok();
    }
    /**
     * 清理
     */
    @RequestMapping("/clean")
    @RequiresPermissions("sys:sysjdbc:clean")
    public R clean(){
        /*DynamicContextHolder.poll();
        DynamicContextHolder.DS_NAME = null;*/
        return R.ok();
    }
}
